package com.jeasonzhao.report.engine.servlet;

import javax.servlet.ServletRequest;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.model.ModelException;
import com.jeasonzhao.model.ModelHelper;
import com.jeasonzhao.report.engine.ReportUserInfo;
import com.jeasonzhao.report.engine.impl.AbstractParameterValueCollector;
import com.jeasonzhao.report.exceptions.ReportRuntimeException;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.model.ReportParam;
import com.jeasonzhao.web.ServletHelper;
import com.jeasonzhao.commons.json.*;
import com.jeasonzhao.report.exceptions.ReportParameterException;

public class ServletParameterCollector extends AbstractParameterValueCollector
{
    //private ConsoleLogger logger = ConsoleLogger.getInstance();
    private ServletRequest request = null;
    public ServletParameterCollector(ServletRequest pc)
    {
        request = pc;
    }

    @Override
    protected boolean getParameterValue(Report report,ReportUserInfo user,ReportParam parameter)
        throws ReportParameterException
    {
        if(null == report || null == parameter)
        {
            return false;
        }
        String key = Constants.getParameterContolName(parameter.getParam());
        String[] argvs = ServletHelper.getParameters(request,key,parameter.getParam());
        if(null != argvs)
        {
            parameter.setValue((Object[]) argvs);
        }
        return false;
    }

    public Report setupReport()
        throws ReportRuntimeException
    {
        String strJSON = ServletHelper.getParameterString(request,Constants.REPORTDEFINATION);
        if(Algorithms.isEmpty(strJSON))
        {
            return null;
        }
        try
        {
            return (Report) ModelHelper.getInstance().fromJSON(Report.class,strJSON);
        }
        catch(ModelException e)
        {
            //Do not handle this exception
        }
        return null;
    }

    public int getPageSize(Report report)
        throws ReportRuntimeException
    {
        return ServletHelper.getParameterInt(request
                                             ,null == report ? 0 : report.getPageSize()
                                             ,Constants.PAGENO);
    }

    public int getPageNo(Report report)
        throws ReportRuntimeException
    {
        return Math.max(0,ServletHelper.getParameterInt(request,0,Constants.PAGENO));
    }

    public String getRenderKey()
    {
        return ServletHelper.getParameterString(request,Constants.RENDERKEY);
    }

    public void collectDynQuerySettings(Report report)
        throws ReportRuntimeException
    {
        String strScript=ServletHelper.getParameterString(request,Constants.DYNAMIC);
        try
        {
            super.collectDynQuerySettingsFromJSONString(report,strScript);
        }
        catch(JSONException ex)
        {
            throw new ReportRuntimeException.WrongDynamicParameterScript(report,strScript,ex);
        }
    }

    public void tearDown(Report report)
    {
    }

}
